package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.service.BpSystemVideoService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.streampusher.StreamPusherManager;
import com.joysuch.wwyt.streampusher.exception.PushException;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 视频流控制器
 *
 * @author qingzhou
 * 2019-12-08
 */
@Slf4j
@RestController
@Api(tags = "视频管理")
@RequestMapping("/bp/stream")
public class BpMonitorStreamController {


    @Autowired
    private StreamPusherManager streamPusherManager;
    @Autowired
    private BpSystemVideoService systemVideoService;



    @PostMapping(value = "streamClose")
    public ResultBean streamClose(String id) {
        streamPusherManager.close(id);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping(value = "streamBreath")
    public ResultBean streamBreath(String id) {
        try {
            streamPusherManager.breath(id);
            return ResultBean.defaultSuccessResult();
        } catch (PushException e) {
            log.error("视频流breath失败 {}", e.getMessage(), e);
            return ResultBean.wrapErrorResultDefault("视频流breath失败");
        }
    }

    /**
     * 打开视频流
     * @param id      摄像头id
     */
    @PostMapping(value = "openStream")
    public ResultBean openStream(Long id, HttpServletRequest request) {
        try {
            return systemVideoService.openStream(id,request);
        } catch (Exception e) {
           log.error("开启直播视频流失败",e);
            return ResultBean.wrapErrorResultDefault("开启直播视频流失败,信息如下：" + e.getMessage());
        }
    }

    @PostMapping(value = "openHistoryStream")
    public ResultBean openHistoryStream(Long id, Date startTime, Date endTime, HttpServletRequest request) {
        try {
            return systemVideoService.openHistoryStream(id,startTime,endTime, request);
        } catch (Exception e) {
            log.error("开启历史视频流出现异常:", e);
            return ResultBean.wrapErrorResultDefault("开启历史视频流失败,信息如下：" + e.getMessage());
        }
    }

}
